#include "Scale.hpp"
#include <Wire.h>
#include <RBD_Timer.h>
#include <RBD_Button.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "font.h"
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

RBD::Button key1(KEY1, INPUT_PULLUP);
RBD::Button key2(KEY2, INPUT_PULLUP);
RBD::Button key3(KEY3, INPUT_PULLUP);

bool Baseflag = 0, Maopiflag = 0;
short Unitflag = 0,Modeflag=0,lineflag=3;
char welcome[]={'W','e','l','c','o','m','e','!'};
float danjia=0,zongjia=0;

void onChangebase() {
  if(Modeflag==2){
    Serial.print(123);
    lineflag--;
    if(lineflag<0)lineflag=3;
    return;
  }
  Get_Base();
  Baseflag = !Baseflag;
}
void onChangemaopi() {
  if(Modeflag==2)
  {
    if(lineflag==3) danjia-=0.1;
    else if(lineflag==2) danjia-=1;
    else if(lineflag==1) danjia-=10;
    else if(lineflag==0) danjia-=100;
    return;
  }
  Get_Maopi();
  Maopiflag = !Maopiflag;

}
void onChangedanwei() {
  if(Modeflag==2)
  {
    if(lineflag==3) danjia+=0.1;
    else if(lineflag==2) danjia+=1;
    else if(lineflag==1) danjia+=10;
    else if(lineflag==0) danjia+=100;
    return;
  }
  Unitflag++;
  if (Unitflag == 6) Unitflag = 0;
}
void onChangeMode(){
  Modeflag++;
  if(Modeflag==3)Modeflag=0;
}
void onChangecalibrate() {
  double CalibrateWeight=
  GapValue = (double(HX711_Read_Stabilized()) - double(Weight_Maopi)) / CalibrateWeight;
}
void printweight(float Weight,int y){
  int yy=0;
  if(y==28) yy=16;
  if(Weight>5000) {display.print("error!");return;}
  if (Unitflag == 0) {display.print(Weight,1);display.setCursor(113, y);display.print('g');}
  else if(Unitflag == 1) {display.print(Weight / 1000);display.setCursor(102, y);display.print("kg");}
  else if(Unitflag == 2) {display.print(Weight / 500);display.drawBitmap(112, yy, gImage_jin, 16, 16, 1);}
  else if(Unitflag == 3) {display.print(Weight / 50);display.drawBitmap(112, yy, gImage_liang, 16, 16, 1);}
  else if(Unitflag == 4) {display.print(Weight / 453.515);display.setCursor(106, y+2);display.print("lb");}
  else if(Unitflag == 5) {display.print(Weight * 0.03527);display.setCursor(104, y+2);display.print("oz");}
}
void printcount(){
  display.setCursor(0, 30);
  if(Get_Count()>1000||Get_Count()<0) display.print(0);
  else display.print(Get_Count());
}
void printdanjia(){
  display.setCursor(0, 30);
  if(danjia<100)
  {
    display.print('0');
    if(danjia<10) display.print('0');
  }
  display.print(danjia,1);
}
void printline(){
  if(lineflag==3) display.drawLine(35,31,43,31,WHITE);
  else if(lineflag==2) display.drawLine(20,31,28,31,WHITE);
  else if(lineflag==1) display.drawLine(10,31,18,31,WHITE);
  else if(lineflag==0) display.drawLine(0,31,8,31,WHITE);
  display.display();
}
void setup() {
  Init_Hx711();  //初始化HX711模块连接的IO设置

  Serial.begin(9600);//设置波特率
  key1.setDebounceTimeout(20);//三个按键的防抖
  key2.setDebounceTimeout(20);
  key3.setDebounceTimeout(20);

  Wire.begin(OLED_SDA, OLED_SCL);  // 初始化OLED并设置其IIC地址为 0x3C
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.setFont(&Roboto_Slab_Regular_16);//设置字体
  display.clearDisplay();//清空屏幕
  display.setTextColor(WHITE);//设置字体颜色
  display.setTextSize(1);//设置字号

  Get_Maopi();//清零

  display.setCursor(20, 21);//动画1
  for(int i=0;i<8;i++)
  {
    display.print(welcome[i]);
    display.display();
    delay(100);
  }
  for(int i=0;i<32;i++)//动画2
  {
    for(int j=0;j<128;j++)
    {
      if(j%4<=1)
      {
        display.drawPixel(j,i,WHITE);
      }
      else
      {
        display.drawPixel(j,31-i,WHITE);
      }
    }
    display.display();
    delay(10);
  }
  display.fillRect(0,0,128,32,WHITE);//动画3
  for(int i=15;i>=0;i--)
  {
    display.fillRect(i*4,i,(16-i)*8,(16-i)*2,BLACK);
    display.display();
    delay(20);
  }

}
void loop() {
  //检测按键
  if (key1.onPressed()) onChangebase();
  if (key2.onPressed()) onChangemaopi();
  if (key3.onPressed()) onChangedanwei();
  if (touchRead(4) <= 50) onChangeMode();
  float Weight=Get_Weight();
  zongjia=Weight*danjia;
  display.clearDisplay();

  display.drawLine(50, 0, 50, 32, WHITE);//分割线

  if (Modeflag) display.drawBitmap(0, 0, gImage_jijia, 32, 16, 1);
  else display.drawBitmap(0, 0, gImage_jishu, 32, 16, 1);
  display.setCursor(56, 14);//右上角
  if (Modeflag) printweight(Weight,14); 
  else printweight(Weight_Base,14);  
  if (Modeflag) printdanjia(); 
  else printcount();
  display.setCursor(56, 30);//右上角
  if (Modeflag) {
    if(zongjia<100000){
      display.print(zongjia);
      display.drawBitmap(112, 16, gImage_yuan, 16, 16, 1);      
    }
    else{
      display.print(zongjia/10000);
      display.drawBitmap(96, 16, gImage_wan, 16, 16, 1);  
      display.drawBitmap(112, 16, gImage_yuan, 16, 16, 1);  
    }
  }
  else printweight(Weight,28); 
  if(Modeflag==2) printline();
  display.display();
}